{% extends "userpage.html" %}

{% block title %}Current Product Orders - {{super()}}{% endblock %}

{% block extrahdrs %}
{% endblock %}


{% block body %}
{% import '_product_list.html' as plist %}

# if notes_updated
	<div class="disappear success plus1" style="position:absolute;top:15px;right:15px;width:250px;text-align:center;">
		Notes updated successfully.
	</div>
# endif

<h3>Product Orders
	# if current_list
	  from {{ localtime(current_list.created).strftime('%B') }} {{ localtime(current_list.created).strftime('%d').strip('0') }} Product List
	# endif
</h3>

# if current_list
	# if current_orders
		<label for="view">Showing</label> &nbsp; <select name="view" id="view">
			<option value="all">-- All Orders --</option>
			# for order in current_orders
				<option value="{{order.key()}}" {{cond(order == active_order, 'selected="selected"')|safe}}>{{order.user.username}}</option>
			# endfor
		</select>
		<script type="text/javascript">
			$('#view').change(function() {
				window.location.href = '/?view='+$(this).val();
			});
			</script>
		&nbsp; &nbsp; &nbsp; Last Updated: 
		# if active_order
			{{active_order.updated|as_time_ago}}
		# else
			{{last_update|safe}}
		# endif
	# else
		-- <strong>No orders received</strong> --
	# endif
	<br /><br />
	<hr />
	
	# if active_order
		<div style="position:absolute;margin-left:370px;white-space:nowrap;">
			{{active_order.user.display_email()}}<br />
			# if active_order.user.phone
				{{active_order.user.phone}}<br />
			# endif
			# if active_order.user.address
				{{active_order.user.address}}
			# endif
		</div>
	# endif
	
	# if not active_order
		<p class="plus1" id="notes-preview">
			{{current_list.render_notes()|safe}}
			<br />
			<button type="button" onclick="$('#notes-preview').hide();$('#update-notes').show();" style="vertical-align:top;margin-top:8px;font-size:1.1em;">Edit Product Notes</button>
		</p>
		<div id="update-notes" style="display:none;">
			<p class="plus1">
				<form method="post">
				<textarea id="notes" name="notes" class="notes">{{current_list.notes}}</textarea>
				<button type="submit" name="action" value="update-notes" style="vertical-align:top;margin-top:8px;font-size:1.1em;">Save Product Notes</button>
				</form>
			</p>
		</div>
		<br />
	# endif
	{{ plist.product_list_table(active_order or all_orders, as_order=True, show_all=all_orders) }}
	
	# if active_order
		<p class="plus1">
			<strong>Notes from Buyer:</strong><br />
			# if not active_order.notes
				-- no notes --
			# else
				{{active_order.notes|plaintext2html|safe}}
			# endif
		</p>
		<br /><br />
		<br /><label for="output">Spreadsheet Output</label>
		<textarea id="output" style="width:100%;height:100px;" onfocus="$(this).select();" onclick="$(this).select();">{{active_order.output()}}</textarea>
	# elif current_orders
		<br />
		<p>
			<strong class="plus1">Notes from Buyers:</strong><br />
			{% for aorder in current_orders %}
				<fieldset>
					<legend style="font-size:1em;">&nbsp; {{aorder.user.username}} &nbsp;</legend>
					# if not aorder.notes
						-- no notes --
					# else
						{{aorder.notes|plaintext2html|safe}}
					# endif
				</fieldset>
			{% endfor %}  
		</p>
		<br />
		<br /><label for="output">Spreadsheet Output</label>
		<textarea id="output" style="width:100%;height:100px;" onfocus="$(this).select();" onclick="$(this).select();">{% for aorder in current_orders %}{{aorder.output(headers=loop.index0==0)}}{% endfor %}  </textarea>

		
	# endif
	
# else
	<br /><br />
	<p class="plus1">No product list available yet, but you can <a href="/upload">upload one</a>.</p> 
# endif

<br /><br />
<br />

<script type="text/javascript">
	// foregoing js validation in favor of just server validation
	setTimeout(function() {
		$('.disappear.success').fadeOut();
	}, 2800);
	setTimeout(function() {
		$('.disappear.error').fadeOut();	
	}, 30 * 1000);
</script>

{% endblock %}